﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using Newtonsoft.Json.Linq;
using NLog;
using System.Threading;

namespace ServerMonitor
{
    static class Program
    {
        private static ILogger logger = LogManager.GetCurrentClassLogger();
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
            logger.Debug("program start");
            init();
            //WMIService wmiService = new WMIService("128.64.151.123", "administrator", "123.zxc");
            //WMIService wmiService = new WMIService("192.168.90.2", "Administrator", "Root123456");
            //WriteLog("CPU",wmiService.getCpuInfo());
            //WriteLog("memory", string.Join(",",wmiService.getMemoryUsage()));
            //WriteLog("disk", string.Join(",",wmiService.getDiskUsage()));
        }

        private static void init()
        {
            try
            {
                //初始化设备监视线程
                initMonitorWorker();
                //初始化SNMP监视线程
                initSnmpWorker();

            }
            catch (Exception e)
            {
                logger.Error("init failed:{0},{1}",e.Message,e.StackTrace.ToString());
            }
        }

        private static void initMonitorWorker()
        {
            MonitorWorker monitorWorker = new MonitorWorker();
            monitorWorker.Worklist = new List<DeviceBean>();
            DeviceListSection deviceListSection = ConfigurationManager.GetSection("deviceListSection") as DeviceListSection;
            foreach (DeviceSection deviceSection in deviceListSection.DeviceSectionContainer)
            {
                WriteLog("init", deviceSection.DeviceName + " " + deviceSection.IpAddress + " " + deviceSection.UserName + " " + deviceSection.Password);
                DeviceBean deviceBean = new DeviceBean();
                convertDeviceSectionToDeviceBean(deviceBean, deviceSection);
                monitorWorker.Worklist.Add(deviceBean);
            }
            monitorWorker.init();
            new Thread(monitorWorker.run).Start();
        }

        private static void initSnmpWorker()
        {
            SnmpWorker snmpWorker = new SnmpWorker();
            snmpWorker.Worklist = new List<SnmpBean>();
            SnmpListSection snmpListSection = ConfigurationManager.GetSection("snmpListSection") as SnmpListSection;
            foreach (SnmpSection snmpSection in snmpListSection.SnmpSectionContainer)
            {
                SnmpBean snmpBean = new SnmpBean();
                convertSnmpSectionToSnmpBean(snmpBean,snmpSection);
                snmpWorker.Worklist.Add(snmpBean);
            }
            snmpWorker.init();
            new Thread(snmpWorker.run).Start();
        }

        private static void convertDeviceSectionToDeviceBean(DeviceBean deviceBean , DeviceSection deviceSection)
        {
            deviceBean.DeviceName = deviceSection.DeviceName;
            deviceBean.IpAddress = deviceSection.IpAddress;
            deviceBean.UserName = deviceSection.UserName;
            deviceBean.Password = deviceSection.Password;
        }

        private static void convertSnmpSectionToSnmpBean(SnmpBean snmpBean, SnmpSection snmpSection)
        {
            snmpBean.DeviceName = snmpSection.DeviceName;
            snmpBean.PortCount = snmpSection.PortCount;
            snmpBean.IpAddress = snmpSection.IpAddress;
            snmpBean.UsedMemoryCommand = snmpSection.UsedMemoryCommand;
            snmpBean.FreeMemoryCommand = snmpSection.FreeMemoryCommand;
            snmpBean.CpuLoadCommand = snmpSection.CpuLoadCommand;
            snmpBean.TemperatureCommand = snmpSection.TemperatureCommand;
            snmpBean.TrapString = snmpSection.TrapString;
            snmpBean.PortStatus = new int[snmpBean.PortCount]; //初始化snmpbean中的端口状态数组
        }

        public static void WriteLog(string param1, string param2)
        {
            System.IO.File.AppendAllText(
            "log.txt", // 日志文件名
            string.Format("{0}\t{1}\t{2}\r\n", DateTime.Now, param1, param2), // 用制表符 \t 分隔字段
            Encoding.Default);
        }
    }
}
